﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MMS.Service
{
    public abstract class BaseService<TEntity> : IService.IBaseService<TEntity> where TEntity : class
    {
        //数据父接口 对象！                                
        public IRespository.IBaseRespository<TEntity> iBaseDal = null;

        public BaseService()
        {
            SetIDAL();
        }
        //专门要求 子类 为业务父类里的 数据父接口赋值！
        public abstract void SetIDAL();

        /// <summary>
        /// 获取一个 线程唯一的 数据仓储对象
        /// </summary>
        public IRespository.IDBSession DBSession
        {
            get
            {
                return DBSessionFactory.GetDbSession();
            }
        }

        public TEntity Single(System.Linq.Expressions.Expression<Func<TEntity, bool>> where)
        {
            return iBaseDal.Single(where);
        }

        public void Add(TEntity Model)
        {
            iBaseDal.Add(Model);
        }

        public void Remove(TEntity Model)
        {
            iBaseDal.Remove(Model);
        }

        public void RemoveBy(System.Linq.Expressions.Expression<Func<TEntity, bool>> where)
        {
            iBaseDal.RemoveBy(where);
        }

        public void Modify(TEntity Model, params string[] updateProperties)
        {
            iBaseDal.Modify(Model, updateProperties);
        }

        public void ModifyBy(System.Linq.Expressions.Expression<Func<TEntity, bool>> where, string[] propertyNames, object[] values)
        {
            iBaseDal.ModifyBy(where, propertyNames, values);
        }

        public IQueryable<TEntity> Where(System.Linq.Expressions.Expression<Func<TEntity, bool>> where)
        {
            return iBaseDal.Where(where);
        }

        public IQueryable<TEntity> Where<TKey>(System.Linq.Expressions.Expression<Func<TEntity, bool>> where, System.Linq.Expressions.Expression<Func<TEntity, TKey>> orderBy, bool isAsc = true)
        {
            return iBaseDal.Where(where, orderBy, isAsc);
        }

        public Model.FormatModel.PageData<TEntity> WherePaged<TKey>(int pageIndex, int pageSize, System.Linq.Expressions.Expression<Func<TEntity, bool>> where, System.Linq.Expressions.Expression<Func<TEntity, TKey>> orderBy, bool isAsc = true)
        {
            return iBaseDal.WherePaged(pageIndex, pageSize, where, orderBy, isAsc);
        }

        public Model.FormatModel.PageData<TEntity> WherePaged<TKey>(int pageIndex, int pageSize, System.Linq.Expressions.Expression<Func<TEntity, bool>> where, System.Linq.Expressions.Expression<Func<TEntity, TKey>> orderBy, bool isAsc = true, params string[] includeNames)
        {
            return iBaseDal.WherePaged(pageIndex, pageSize, where, orderBy, isAsc, includeNames);
        }

        public IEnumerable<TElement> SqlQuery<TElement>(string sql, params object[] parameters)
        {
            return iBaseDal.SqlQuery<TElement>(sql, parameters);
        }
        public IEnumerable SqlQuery(Type elementType, string sql, params object[] parameters)
        {
            return iBaseDal.SqlQuery(elementType, sql, parameters);
        }
    }
}
